/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.project.web.project;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.utils.Constant;
import com.idenger.modules.project.entity.projectbasicinfo.ProjectBasicinfo;
import com.idenger.modules.project.entity.projecttype.ProjectType;
import com.idenger.modules.project.service.projectbasicinfo.ProjectBasicinfoService;
import com.idenger.modules.project.service.projecttype.ProjectTypeService;
import com.idenger.modules.sys.entity.User;
import com.idenger.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.project.entity.project.Project;
import com.idenger.modules.project.service.project.ProjectService;

import java.util.List;

/**
 * 项目Controller
 * @author chenlin
 * @version 2018-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/project/project")
public class ProjectController extends BaseController {

	@Autowired
	private ProjectService projectService;
	@Autowired
	private ProjectTypeService projectTypeService;

	@Autowired
	private ProjectBasicinfoService projectBasicinfoService;
	
	@ModelAttribute
	public Project get(@RequestParam(required=false) String id) {
		Project entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectService.get(id);
		}
		if (entity == null){
			entity = new Project();
		}
		return entity;
	}
	
	@RequiresPermissions("project:project:project:view")
	@RequestMapping(value = {"list", ""})
	public String list(Project project, HttpServletRequest request, HttpServletResponse response, Model model) {

		//查询项目列表
		String currentId = UserUtils.getUser().getId();
		if(StringUtils.isNotBlank(currentId)){
			if (currentId.equals("1")){
				project.setCreatorId("");
			}else{
				project.setCreatorId(currentId);
			}
		}
		String projectName = request.getParameter("projectName");
		if(StringUtils.isNotBlank(projectName)){
			project.setProjectName(projectName);
		}



		Page<Project> page = projectService.findPage(new Page<Project>(request, response), project);

		model.addAttribute("page", page);
		return "modules/project/project/projectList";
	}

	@RequiresPermissions("project:project:project:view")
	@RequestMapping(value = "form")
	public String form(Project project, Model model) {
		//查询项目分类
		List<ProjectType> projectTypeList = projectTypeService.findList(new ProjectType());
		ProjectBasicinfo projectBasicinfo = projectBasicinfoService.getProjectBasicinfoByprojectId(project.getId());
		model.addAttribute("projectTypeList",projectTypeList);
		model.addAttribute("project", project);
		model.addAttribute("projectBasicinfo", projectBasicinfo);
		return "modules/project/projectbasicinfo/projectBasicinfoForm";
	}

	@RequiresPermissions("project:project:project:edit")
	@RequestMapping(value = "save")
	public String save(Project project, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, project)){
			return form(project, model);
		}
		//获取当前登录者
		project.setCreatorId(UserUtils.getUser().getId());
		project.setApprovalStatus(Constant.APPROVAL_STATUS_UNAPPROVED);//未审批状态
		project.setDeleteFlag(Constant.DELETE_STATUS);
		projectService.save(project);
		addMessage(redirectAttributes, "保存项目成功");
		return "redirect:"+Global.getAdminPath()+"/project/project/project/?repage";
	}
	
	@RequiresPermissions("project:project:project:edit")
	@RequestMapping(value = "delete")
	public String delete(Project project, RedirectAttributes redirectAttributes) {

		//删除项目

		projectService.delete(project);
		addMessage(redirectAttributes, "删除项目成功");
		return "redirect:"+Global.getAdminPath()+"/project/project/project/?repage";
	}

}